package com.wanmait.recycle.webcontroller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.City;
import com.wanmait.recycle.pojo.Data;
import com.wanmait.recycle.pojo.Province;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Unit;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.CityService;
import com.wanmait.recycle.service.DataService;
import com.wanmait.recycle.service.ProvinceService;
import com.wanmait.recycle.service.RubbishService;


@Controller
@RequestMapping("web/userinfo")
public class WebDataController {

	@Resource
	private DataService dataService;
	@Resource
	private RubbishService rubbishService;
	@Resource
	private ProvinceService provinceService;
	@Resource
	private CityService cityService;
	
	
	@RequestMapping(value="addData",method=RequestMethod.GET)
	public void addData2()
	{
		
	}
	
	@RequestMapping(value="addData",method=RequestMethod.POST)
	public void addData(Data data)
	{
		dataService.addData(data);
	}
	
	
	@RequestMapping("seachData")
	@ResponseBody
	public List<Rubbish> seachData(String seachData)
	{
		 return rubbishService.findAllBySearch(seachData);
	}
	
	@RequestMapping("dataList")
	public ModelAndView data(HttpSession session) {
		
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		try {
			if(userinfo.getUsertype().getId()==1) {
				mv.setViewName("redirect:/web/userinfo/yanZheng?msg=common&id="+userinfo.getId());
			}else {
				PageInfo<Data> dataList = dataService.findAllById(userinfo.getId());
				mv.addObject("dataList", dataList);
			}
		} catch (Exception e) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}
	 	return mv;
		
	}
	
	@RequestMapping("dataDelect")
	@ResponseBody
	public void dataDelect(Integer id) {
		
	 	dataService.delete(id);
		
	}
	
	
	@RequestMapping("changeData")
	public ModelAndView changeData(Integer id) {
		ModelAndView mv = new ModelAndView();
		Data data = dataService.findById(id);
		Rubbish rubbish = rubbishService.selectById(data.getRubbish().getId());
		mv.addObject("rubbish", rubbish);
		List<Province> provinces = provinceService.findAll();
		mv.addObject("provinces", provinces);
		mv.addObject("theData", data);
		mv.setViewName("/web/userinfo/addData");
		return mv;
	}
	
	@RequestMapping("seachByTime")		
	public ModelAndView seachByTime(String beginTime,String endTime,HttpSession session) {
	
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo =(Userinfo)session.getAttribute("userinfo");
		PageInfo<Data> dataList = dataService.seachByTime(beginTime,endTime,userinfo.getId());
		mv.addObject("dataList", dataList);
		mv.setViewName("/web/userinfo/dataList");
		return mv;		
	}
	
	@RequestMapping("accept")
	@ResponseBody
	public ModelAndView accept(Integer id) {
		
		ModelAndView mv = new ModelAndView();
		Rubbish rubbish = rubbishService.selectById(id);
		
		mv.addObject("rubbish", rubbish);
		List<Province> provinces = provinceService.findAll();
		mv.addObject("provinces", provinces);
		
		mv.setViewName("/web/includ/dataList");
		
		return mv;
	}
	
	@RequestMapping("findAllByProvinceId")
	@ResponseBody
	public List<City> findAllByProvinceId(Integer provinceId) {

		List<City> cities = cityService.findAllByProvinceId(provinceId);
		
		return cities;
	}
	
	@RequestMapping("saveData")
	public String saveData(Data data,String startTime,String stopTime,String chanSheng,String huiShou,HttpSession session) {		
		
		try {
			boolean f = true;
			if(huiShou=="") {
				f=false;
				data.setCount(Double.parseDouble(chanSheng));
			}
			if(chanSheng=="") {
				f=true;
				data.setCount(Double.parseDouble(huiShou));
			}
			data.setIsDispose(f);
		} catch (NumberFormatException e) {
			return "redirect:/web/userinfo/addData?msg=Num";
		}
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		data.setUserinfo(userinfo);
		
		dataService.saveData(data,startTime,stopTime);
		
		return "redirect:/web/userinfo/addData?msg=succ";
		
		
	}
	@RequestMapping("change")
	public String change(Data data,String startTime,String stopTime,String chanSheng,String huiShou,HttpSession session) {		
		
		try {
			boolean f = true;
			if(huiShou==null) {
				
				f=false;
				data.setCount(Double.parseDouble(chanSheng));
			}
			if(chanSheng==null) {
				
				f=true;
				data.setCount(Double.parseDouble(huiShou));
			}
			data.setIsDispose(f);
		} catch (NumberFormatException e) {
			return "redirect:/web/userinfo/dataList?msg=Num";
		}
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		data.setUserinfo(userinfo);
	
		dataService.changeData(data,startTime,stopTime);
		
		return "redirect:/web/userinfo/dataList?msg=succ";
		
		
	}
	
}
